package com.huqu.beans;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import org.primefaces.component.fileupload.FileUpload;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;

import com.huqu.dao.N100Dao;
import com.huqu.dao.N300Dao;
import com.huqu.entities.N100;
import com.huqu.entities.N300;
import com.huqu.utils.ApplicationConstant;
import com.huqu.utils.ApplicationHelper;

@ManagedBean(name = "addnewsBean")
@ViewScoped
public class AddNewsBean implements Serializable {
	private static final long serialVersionUID = 8692624075515813353L;

	List<N100> cboListN100=new ArrayList<N100>();
	List<N100> listn100=new ArrayList<N100>();
	List<N300> listN300=new ArrayList<N300>();
	private UploadedFile fileimage;
	private N300 n300insert = new N300();
	private N100 n100insert = new N100();
	private String vLang="VN";
	private int idmuc=1;
	/*private String stridtheloai = "";*/
	
	@PostConstruct
	public void init(){
		try {
			loadCboN100(idmuc);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	// load cbo cac the loai ung voi cac muc
	public void loadCboN100(int idmuc){
		try {
			N100Dao n100Dao = (N100Dao) ApplicationHelper.findBean(ApplicationConstant.REPOSITORY_NAME_N100);
			System.out.println("---ListOfTabN100( "+idmuc+", 0, "+ vLang+" )");
			cboListN100=n100Dao.ListOfTabN100(idmuc, 0, vLang);
			System.out.println("ListN100.size()= "+cboListN100.size());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void changeCboMuc(){
		System.out.println("change cbo muc");
		loadCboN100(idmuc);
	}
	
	public void insertTabN100() {
		try {

			N100Dao n100Dao = (N100Dao) ApplicationHelper
					.findBean(ApplicationConstant.REPOSITORY_NAME_N100);
			n100insert.setIdtheloai(0);
			n100insert.setNguoitao(1);
			n100insert.setKiemduyet(1);
			n100insert.setDaxoa(0);
			System.out.println("--- InsertTabN100(" + n100insert.getIdtheloai()
					+ " , " + n100insert.getIdmuc() + " , "
					+ n100insert.getTentheloai_vi() + " , "
					+ n100insert.getTentheloai_en() + " , "
					+ n100insert.getHinhanh_name() + " , "
					+ n100insert.getNguoitao() + " , " + n100insert.getDaxoa()
					+ " , " + n100insert.getKiemduyet() + " , "
					+ n100insert.getUutien() + ")");

			int result = n100Dao.InsertTabN100(0,
					n100insert.getIdmuc(), n100insert.getTentheloai_vi(),
					n100insert.getTentheloai_en(), n100insert.getHinhanh_name(),
					n100insert.getNguoitao(), n100insert.getDaxoa(),
					n100insert.getKiemduyet(), n100insert.getUutien());
			
			System.out.println("--- result= " + result);
			if (result > 0) {
				ApplicationHelper.showMessageSuccess("Cập nhật thành công.");
				n300insert = new N300();
			} else {
				ApplicationHelper.showMessageErro("Cập nhật thất bại");
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void insertTabN300() {
		try {
			/*
			 * int rs= ApplicationHelper.copyFileImage(fileimage.getFileName(),
			 * fileimage.getInputstream()); if (rs > 0) {
			 * ApplicationHelper.showMessageSuccess("upload thành công.");
			 * n300insert.setHinhanh(fileimage.getFileName()); } else {
			 * ApplicationHelper.showMessageErro("upload thất bại"); }
			 */

			N300Dao n300Dao = (N300Dao) ApplicationHelper
					.findBean(ApplicationConstant.REPOSITORY_NAME_N300);
			n300insert.setIdbaiviet(0);
			n300insert.setNguoitao(1);
			n300insert.setKiemduyet(1);
			n300insert.setDaxoa(0);
			System.out.println("--- InsertTabN300(" + n300insert.getIdbaiviet()
					+ " , " + n300insert.getIdtheloai() + " , "
					+ n300insert.getTieude_vi() + " , "
					+ n300insert.getTieude_en() + " , "
					+ n300insert.getTomtat_vi() + " , "
					+ n300insert.getTomtat_en() + " , "
					+ n300insert.getNoidung_vi() + " , "
					+ n300insert.getNoidung_en() + " , "
					+ n300insert.getHinhanh_name() + " , "
					+ n300insert.getNguoitao() + " , " + n300insert.getDaxoa()
					+ " , " + n300insert.getKiemduyet() + " , "
					+ n300insert.getUutien() + ")");

			int result = n300Dao.InsertTabN300(n300insert.getIdbaiviet(),
					n300insert.getIdtheloai(), n300insert.getTieude_vi(),
					n300insert.getTieude_en(), n300insert.getTomtat_vi(),
					n300insert.getTomtat_en(), n300insert.getNoidung_vi(),
					n300insert.getNoidung_en(), n300insert.getHinhanh_name(),
					n300insert.getNguoitao(), n300insert.getDaxoa(),
					n300insert.getKiemduyet(), n300insert.getUutien());

			System.out.println("--- result= " + result);
			if (result > 0) {
				ApplicationHelper.showMessageSuccess("Cập nhật thành công.");
				n300insert = new N300();
			} else {
				ApplicationHelper.showMessageErro("Cập nhật thất bại");
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void uploadimage(FileUploadEvent event) {
		try {
			int result = ApplicationHelper.copyFileImage(event.getFile()
					.getFileName(), event.getFile().getInputstream());
			if (result > 0) {
				ApplicationHelper.showMessageSuccess("upload thành công.");
				n300insert.setHinhanh_name(event.getFile().getFileName());
			} else {
				ApplicationHelper.showMessageErro("upload thất bại");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void uploadimage_theloai(FileUploadEvent event) {
		try {
			String address="E:\\Khoa Luan\\workspace\\huongnghiep\\src\\main\\webapp\\images\\upload\\theloai\\";
			int result = ApplicationHelper.copyFileImage1(event.getFile()
					.getFileName(), event.getFile().getInputstream(),address);
			if (result > 0) {
				ApplicationHelper.showMessageSuccess("upload thành công.");
				n100insert.setHinhanh_name(event.getFile().getFileName());
			} else {
				ApplicationHelper.showMessageErro("upload thất bại");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public N300 getN300insert() {
		return n300insert;
	}

	public void setN300insert(N300 n300insert) {
		this.n300insert = n300insert;
	}

	/*public int getIdtheloai() {
		return idtheloai;
	}

	public void setIdtheloai(int idtheloai) {
		this.idtheloai = idtheloai;
	}

	public String getStridtheloai() {
		return stridtheloai;
	}

	public void setStridtheloai(String stridtheloai) {
		this.stridtheloai = stridtheloai;
	}*/

	public UploadedFile getFileimage() {
		return fileimage;
	}

	public void setFileimage(UploadedFile fileimage) {
		this.fileimage = fileimage;
	}

	public List<N100> getListn100() {
		return listn100;
	}

	public void setListn100(List<N100> listn100) {
		this.listn100 = listn100;
	}

	public List<N300> getListN300() {
		return listN300;
	}

	public void setListN300(List<N300> listN300) {
		this.listN300 = listN300;
	}

	public List<N100> getCboListN100() {
		return cboListN100;
	}

	public void setCboListN100(List<N100> cboListN100) {
		this.cboListN100 = cboListN100;
	}

	public N100 getN100insert() {
		return n100insert;
	}

	public void setN100insert(N100 n100insert) {
		this.n100insert = n100insert;
	}

	public int getIdmuc() {
		return idmuc;
	}

	public void setIdmuc(int idmuc) {
		this.idmuc = idmuc;
	}

	
}
